package org.open.sharedRadar;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class WebServiceHandler {
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.168.137.1/Service.asmx";
	
	public String performLogin(String user_, String pass_)
	{
		String  result = "Eroare > ";
		
		final String SOAP_ACTION = "http://tempuri.org/loginUser";
	    final String METHOD_NAME = "loginUser";
	    Object resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    
	   request.addProperty("userName_", user_);
	   request.addProperty("pass_",pass_);
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(request);
	  
	
	   AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	   try
	   {
	       androidHttpTransport.call(SOAP_ACTION, envelope);
	       resultsRequestSOAP =  envelope.getResponse();
	       result = resultsRequestSOAP.toString();            
	   }
	   catch (Exception aE)
	   {
	       aE.printStackTrace ();
	       result = result + aE.toString();
	   }
	return result;
			
	}
	public String performSignUp(String name_, String user_, String pass_)
	{
		String  result = "Eroare > ";
		
		final String SOAP_ACTION = "http://tempuri.org/addUser";
	    final String METHOD_NAME = "addUser";
	    Object resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);    	    
	    request.addProperty("userName_", user_);
	    request.addProperty("pass_",pass_);
	    request.addProperty("name_", name_);
	    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    envelope.dotNet = true;
	    envelope.setOutputSoapObject(request);
	  
	
	    AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	    try
	    {
	    	androidHttpTransport.call(SOAP_ACTION, envelope);
	    	resultsRequestSOAP =  envelope.getResponse();
	    	result = resultsRequestSOAP.toString();            
	    }
	    catch (Exception aE)
	    {
	    	aE.printStackTrace ();
	    	result = result + aE.toString();
	    }
	return result;
			
	}
	public SoapObject getRadars()
	{
	   SoapObject  result = null;
	   final String SOAP_ACTION = "http://tempuri.org/getRadare";
	    final String METHOD_NAME = "getRadare";
	    
	    SoapObject resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
	    Double longitude_ = CurentUserProperties.getInstance().myCurrentLocation.getLongitude();
	    Double latitude_ = CurentUserProperties.getInstance().myCurrentLocation.getLatitude();
	   String latitude = latitude_.toString();
	   String longitude = longitude_.toString();
	   String radius_ = CurentUserProperties.getInstance().myRadius.toString();
	   Integer radius = Integer.parseInt(radius_);
	    request.addProperty("longStr_", longitude);
	   request.addProperty("latStr_",latitude);
	   request.addProperty("razaMetri_",radius);
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(request);
	  
	
	   AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	   try
	   {
	       androidHttpTransport.call(SOAP_ACTION, envelope);
	       resultsRequestSOAP =  (SoapObject) envelope.bodyIn;
	       for(int i= 0; i< resultsRequestSOAP.getPropertyCount(); i++)
	       {
	    	   SoapObject soapList = (SoapObject) resultsRequestSOAP.getProperty(i);	    	       		   	    			  
	    	   result = soapList;
	    	   return result;			           	   
	       }
	       
	       //result = resultsRequestSOAP..toString();            
	   }
	   catch (Exception aE)
	   {
	       aE.printStackTrace ();
	       result = null;
	   }
	return result;
			
	}
	public SoapObject getFriends()
	{
	   SoapObject  result = null;
	   final String SOAP_ACTION = "http://tempuri.org/getPrieteni";
	    final String METHOD_NAME = "getPrieteni";
	    
	    SoapObject resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    
	   
	    String idSolicitant = CurentUserProperties.getInstance().currentUserId;
	    Double longitude_ = CurentUserProperties.getInstance().myCurrentLocation.getLongitude();
	    Double latitude_ = CurentUserProperties.getInstance().myCurrentLocation.getLatitude();
	    String latitude = latitude_.toString();
	   String longitude = longitude_.toString();
	   String radius_ = CurentUserProperties.getInstance().myRadius.toString();
	   Integer radius = Integer.parseInt(radius_);
	   request.addProperty("idSolicitant_", idSolicitant);
	    request.addProperty("longStr_", longitude);
	   request.addProperty("latStr_",latitude);
	   request.addProperty("razaMetri_",radius);
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(request);
	  
	
	   AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	   try
	   {
	       androidHttpTransport.call(SOAP_ACTION, envelope);
	       resultsRequestSOAP =  (SoapObject) envelope.bodyIn;
	       for(int i= 0; i< resultsRequestSOAP.getPropertyCount(); i++)
	       {
	    	   SoapObject soapList = (SoapObject) resultsRequestSOAP.getProperty(i);	    	       		   	    			  
	    	   result = soapList;
	    	   return result;			           	   
	       }	       	             
	   }
	   catch (Exception aE)
	   {
	       aE.printStackTrace ();
	       result = null;
	   }
	return result;
			
	}
	public String reportRadar(Boolean fix_, Boolean inexistent_)
	{
		String  result = "Va multumim";
	   final String SOAP_ACTION = "http://tempuri.org/reportRadar";
	    final String METHOD_NAME = "reportRadar";
	    
	    Object resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
	    Double longitude_ = CurentUserProperties.getInstance().myCurrentLocation.getLongitude();
	    Double latitude_ = CurentUserProperties.getInstance().myCurrentLocation.getLatitude();
	   String latitude = latitude_.toString();
	   String longitude = longitude_.toString();	   
	   String idSolicitant = CurentUserProperties.getInstance().currentUserId;
	   request.addProperty("userName_",idSolicitant);
	   request.addProperty("radarFix_",fix_);
	   request.addProperty("longStr_", longitude);
	   request.addProperty("latStr_",latitude);
	   request.addProperty("inexistent_",inexistent_);
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(request);
	  
	
	   AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	   try
	   {
	       androidHttpTransport.call(SOAP_ACTION, envelope);
	       resultsRequestSOAP =  envelope.getResponse();
	       Boolean rez = Boolean.parseBoolean(resultsRequestSOAP.toString());  
	       if(!rez)
	    	   result = "Ati raport deja acest radar!";	       	         
	   }
	   catch (Exception aE)
	   {
	       aE.printStackTrace ();
	       result = "A aparut o eroare. Incercati mai tarziu!";
	   }
	return result;
			
	}
	public String confirmIgnoreFriends(String idUtilizatorVizat_, Boolean ignore_)
	{
		String  result = "Va multumim";
	   final String SOAP_ACTION = "http://tempuri.org/confirmaIgnoraCererePrietenie";
	    final String METHOD_NAME = "confirmaIgnoraCererePrietenie";
	    
	    Object resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	     
	   String idSolicitant = CurentUserProperties.getInstance().currentUserId;
	   request.addProperty("idSolicitant_",idSolicitant);
	   request.addProperty("idUtilizatorVizat_",idUtilizatorVizat_);
	   request.addProperty("ignora_",ignore_);
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(request);
	  
	
	   AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	   try
	   {
	       androidHttpTransport.call(SOAP_ACTION, envelope);
	       resultsRequestSOAP =  envelope.getResponse();
	       result = resultsRequestSOAP.toString();  	        	         
	   }
	   catch (Exception aE)
	   {
	       aE.printStackTrace ();
	       result = "A aparut o eroare. Incercati mai tarziu!";
	   }
	return result;
			
	}
	
	public SoapObject getFriendsRequests()
	{
	   SoapObject  result = null;
	   final String SOAP_ACTION = "http://tempuri.org/getCereriPrietenie";
	    final String METHOD_NAME = "getCereriPrietenie";
	    
	    SoapObject resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	    
	   
	    String idSolicitant = CurentUserProperties.getInstance().currentUserId;
	
	   request.addProperty("idSolicitant_", idSolicitant);	    
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(request);
	  
	   AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	   try
	   {
	       androidHttpTransport.call(SOAP_ACTION, envelope);
	       resultsRequestSOAP =  (SoapObject) envelope.bodyIn;
	       for(int i= 0; i< resultsRequestSOAP.getPropertyCount(); i++)
	       {
	    	   SoapObject soapList = (SoapObject) resultsRequestSOAP.getProperty(i);	    	       		   	    			  
	    	   result = soapList;
	    	   return result;			           	   
	       }
	       
	       //result = resultsRequestSOAP..toString();            
	   }
	   catch (Exception aE)
	   {
	       aE.printStackTrace ();
	       result = null;
	   }
	return result;
	}
	
	public String sendFriendRequest(String idUtilizatorVizat_)
	{
		String  result = "Va multumim";
	   final String SOAP_ACTION = "http://tempuri.org/adaugaCererePrietenie";
	    final String METHOD_NAME = "adaugaCererePrietenie";
	    
	    Object resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);	     
	   String idSolicitant = CurentUserProperties.getInstance().currentUserId;
	   request.addProperty("idSolicitant_",idSolicitant);
	   request.addProperty("idUtilizatorVizat_",idUtilizatorVizat_);	   
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(request);
	  	
	   AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	   try
	   {
	       androidHttpTransport.call(SOAP_ACTION, envelope);
	       resultsRequestSOAP =  envelope.getResponse();
	       result = resultsRequestSOAP.toString();  	        	         
	   }
	   catch (Exception aE)
	   {
	       aE.printStackTrace ();
	       result = "A aparut o eroare. Incercati mai tarziu!";
	   }
	return result;
			
	}
	
	public void updateLocation()
	{		
	   if(CurentUserProperties.getInstance().myCurrentLocation == null 
			   || CurentUserProperties.getInstance().myCurrentLocation.getLongitude() == 0 
			   || CurentUserProperties.getInstance().myCurrentLocation.getLatitude() == 0)
		   return;
		final String SOAP_ACTION = "http://tempuri.org/updateLocation";
	    final String METHOD_NAME = "updateLocation";
	    
	    Object resultsRequestSOAP = null;
	    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
	    Double longitude_ = CurentUserProperties.getInstance().myCurrentLocation.getLongitude();
	    Double latitude_ = CurentUserProperties.getInstance().myCurrentLocation.getLatitude();
	   String latitude = latitude_.toString();
	   String longitude = longitude_.toString();	   
	   String idSolicitant = CurentUserProperties.getInstance().currentUserId;
	   request.addProperty("userName_",idSolicitant);	  
	   request.addProperty("longStr_", longitude);
	   request.addProperty("latStr_",latitude);
	   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	   envelope.dotNet = true;
	   envelope.setOutputSoapObject(request);
	  
	
	   AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
	   try
	   {
	       androidHttpTransport.call(SOAP_ACTION, envelope);
	       resultsRequestSOAP =  envelope.getResponse();
	       String res = resultsRequestSOAP.toString();  	          	         
	   }
	   catch (Exception aE)
	   {
	       aE.printStackTrace ();	    
	   }
			
	}
	
}
